package com.wwj.feedback.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wwj.common.constant.CommonCodeEnum;
import com.wwj.common.constant.CommonConstant;
import com.wwj.common.entitiy.R;
import com.wwj.common.utils.TokenCheck;
import com.wwj.feedback.constant.ConstantFinal;
import com.wwj.feedback.entitiy.FeedbackQuestionEntity;
import com.wwj.feedback.service.FeedbackQuestionService;
import com.wwj.feedback.vo.FeedbackVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/feedbackQuestion")
public class FeedbackQuestionController {

    @Resource
    private FeedbackQuestionService feedbackQuestionService;

    /**
     * 获取未处理的问题
     */
    @RequestMapping("/getNoHandleQuestion")
    private int getNoHandleQuestion(){
        return feedbackQuestionService.getNoHandleQuestion();
    }

    /**
     * 获取反馈所有问题
     * http://127.0.0.1:88/api/feedbackQuestion/getFeedbackData/1/8?query= xxx
     */
    @GetMapping("getFeedbackData/{currentPage}/{pageSize}")
    private R<PageInfo<FeedbackQuestionEntity>> getFeedbackData(@PathVariable("currentPage") String currentPage,
                                      @PathVariable("pageSize") String pageSize,
                                      @RequestParam("query") String query){
        PageInfo<FeedbackQuestionEntity> pageInfo = feedbackQuestionService.selectAllFeedbackData(currentPage,pageSize,query);
        return R.ok(CommonCodeEnum.SUCCESS.getCode(), CommonCodeEnum.SUCCESS.getMsg(), pageInfo);
    }

    /**
     * 根据id删除单条记录
     * http://127.0.0.1:88/api/feedbackQuestion/removeOne?questionId=2
     */
    @DeleteMapping("/removeOne")
    private R<Object> removeOne(@RequestParam("questionId") String questionId) {
        boolean flag = feedbackQuestionService.removeById(questionId);
        if (flag){
            return R.ok("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    /**
     * 反馈内容
     * http://127.0.0.1:88/api/feedbackQuestion/answer
     */
    @PutMapping("/answer")
    private R<Object> answer(@RequestBody FeedbackQuestionEntity feedbackQuestionEntity) throws ParseException {
        feedbackQuestionEntity.setQuestionIdentify(ConstantFinal.VALUE_QUESTION_IDENTIFY_ANSWER);
        feedbackQuestionEntity.setFinishTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))));
        boolean flag = feedbackQuestionService.updateById(feedbackQuestionEntity);
        if (flag){
            return R.ok("更新成功");
        }else {
            return R.error("更新失败");
        }
    }

    /**
     * 反馈表中该用户是否存在
     * OpenFeign
     */
    @GetMapping("/userExist")
    public R<Object> userExist(@RequestParam("ids") List<String> ids){
        for (String userId : ids) {
            QueryWrapper<FeedbackQuestionEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(ConstantFinal.FIELD_USER_ID, userId);
            List<FeedbackQuestionEntity> list = feedbackQuestionService.list(queryWrapper);
            if (list.size() != 0){
                return R.error(CommonCodeEnum.USER_EXIST.getCode(), CommonCodeEnum.USER_EXIST.getMsg());
            }
        }
        return R.ok("可以删除该用户");
    }

    /**
     * 获取该用户的所有反馈信息
     * http://192.168.0.115:88/api/feedbackQuestion/getFeedbackDataForUser
     */
    @GetMapping("/getFeedbackDataForUser")
    private R<Object> getFeedbackDataForUser(HttpServletRequest request,String query){
        String token = request.getHeader(CommonConstant.REQUEST_HEADER_TOKEN);
        // 解析该token的用户id
        String userId = TokenCheck.getUserIdByToken(token);
        QueryWrapper<FeedbackQuestionEntity> queryWrapper = new QueryWrapper<>();
        if (!query.isEmpty()){
            queryWrapper.like(ConstantFinal.FIELD_QUESTION_NUMBER, query);
        }
        queryWrapper.eq(ConstantFinal.FIELD_USER_ID, userId);
        queryWrapper.orderByDesc(ConstantFinal.FIELD_QUESTION_IDENTIFY);
        List<FeedbackQuestionEntity> list = feedbackQuestionService.list(queryWrapper);
        return R.ok(CommonCodeEnum.SUCCESS.getCode(), CommonCodeEnum.SUCCESS.getMsg(), list);
    }

    /**
     * 微信小程序提交反馈
     * http://192.168.0.115:88/api/feedbackQuestion/add
     */
    @PostMapping("/add")
    private R<Object> add(HttpServletRequest request,@RequestBody FeedbackQuestionEntity feedbackQuestionEntity){
        String token = request.getHeader(CommonConstant.REQUEST_HEADER_TOKEN);
        // 解析该token的用户id
        String userId = TokenCheck.getUserIdByToken(token);
        feedbackQuestionEntity.setQuestionIdentify(ConstantFinal.VALUE_QUESTION_NO_ANSWER);
        feedbackQuestionEntity.setCreateTime(new Date());
        feedbackQuestionEntity.setUserId(userId);
        feedbackQuestionService.save(feedbackQuestionEntity);
        return R.ok("增加成功");
    }
}
